package com.creatorblue.controller;

import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.google.code.kaptcha.impl.DefaultKaptcha;

/**
 * 登陆控制层类
 * @author zhuq
 *
 */
@RestController
public class LoginController {
	
	@Autowired
	private DefaultKaptcha defaultKaptcha;
	
	/**
	 * 获取图形验证码 <img th:src="@{/ecode}" />
	 * @param session
	 * @param response
	 * @throws Exception
	 */
	@RequestMapping("ecode")
	public void code(HttpSession session, HttpServletResponse response) throws Exception{
		//生成验证码文本内容
		String code = defaultKaptcha.createText();
		//将验证码文本内容保存到会话中，用于后续登陆时做验证码校验
		session.setAttribute("code", code);
		//使用验证码文本生成一张图片
		BufferedImage image = defaultKaptcha.createImage(code);
		//将图片写到输出流返回给前端img标签展示
		ImageIO.write(image, "jpg", response.getOutputStream());
	}
	
	/**
	 * 打开登陆界面
	 * @return
	 */
	@RequestMapping("login")
	public ModelAndView login(HttpSession session) {
		return new ModelAndView("login");
	}
	
	
}
